package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.DealModel;
import models.DealStatistic;
import models.OrderModel;
import search.DealSearchData;
import search.OrderSearchData;

@SuppressWarnings("serial")
public class ManageDealServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(ManageDealServlet.class
			.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		process(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			int page = 1;
			try {
				page = Integer.parseInt((String) request.getParameter("page"));
			} catch (Exception e) {
				page = 1;
			}
			String strDealId = request.getParameter("dealId");
			DealSearchData dealSearch = new DealSearchData();
			dealSearch.setPage(page);
			List<DealStatistic> listDealStatistic = new ArrayList<DealStatistic>();
			if (strDealId != null && !strDealId.isEmpty()) {
				dealSearch.searchById(strDealId);
				request.setAttribute("dealId", strDealId);
			} else {
				String strName = request.getParameter("name");
				String strUsername = request.getParameter("username");
				String strStatus = request.getParameter("status");
				int status = 0;
				try {
					status = Integer.parseInt(strStatus);
					request.setAttribute("status", strStatus);
				} catch (Exception e) {
					status = 99;
				}
				if (strUsername != null && strUsername.equals("all")) {
					strUsername = null;
				}
				
				dealSearch.searchAdvance(strName, strUsername, null, status);
				request.setAttribute("name", strName);
				request.setAttribute("username", strUsername);
				request.setAttribute("status", strStatus);
			}			
			if (dealSearch.getListResult().size() > 0) {
				for (DealModel deal : dealSearch.getListResult()) {					
					OrderSearchData orderSearch = new OrderSearchData();
					orderSearch.searchByDealId(deal.getDealId());
					DealStatistic dealStatistic = new DealStatistic();
					int registed = 0;
					int delivered = 0;
					int paid = 0;
					int transporting = 0;
					if (orderSearch.getListResult().size() > 0) {
						for (OrderModel order : orderSearch.getListResult()) {
							if (order.getStatus() != 5) {
								registed += order.getQuantity();
								if (order.getStatus() == 4) {
									delivered += order.getQuantity();
									paid += order.getQuantity();
								} else if (order.getStatus() == 3) {
									transporting++;
								}
							}
						}
					}
					dealStatistic.setOrderQuantity(registed);
					dealStatistic.setDeliveredQuantity(delivered);
					dealStatistic.setPaidQuantity(paid);
					dealStatistic.setTransportingQuantity(transporting);
					dealStatistic.setDealId(deal.getDealId());
					dealStatistic.setDealAlias(deal.getAlias());
					dealStatistic.setDealName(deal.getShortName());
					dealStatistic.setDealStatus(deal.getStatus());
					dealStatistic.setMaximum(deal.getLimit());
					dealStatistic.setStartTime(deal.getStartTime());
					dealStatistic.setEndTime(deal.getEndTime());
					listDealStatistic.add(dealStatistic);
				}
			}
			request.setAttribute("listDealStatistic", listDealStatistic);
			request.setAttribute("isHasPreviousPage",
					dealSearch.isHasPreviousPage());
			request.setAttribute("isHasNextPage", dealSearch.isHasNextPage());
			request.setAttribute("page", dealSearch.getPage());
			request.setAttribute("url", request.getRequestURL().toString());
			request.getRequestDispatcher("/admin/pages/deal_manager.jsp")
					.forward(request, response);
		} catch (Exception e) {
			System.out.println("Error when open deal manager page !");
			log.warning("Error when open deal manager page !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
